<?php

namespace App\Services\Api;

use App\Models\Region;
use App\Models\User;
use App\Models\UserAddress;
use App\Models\UserFeedback;
use App\Models\UserFeedbackImage;
use App\Services\Api\ApiService;
use DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

/**
 * 用户接口模块
 * return ['status'=>1成功2失败,'msg'=>'提示信息','data'=>'返回数据']
 */
class UserService extends ApiService
{

    #################用户设置####################

    /**
     * 用户登录
     * @param [type] $phone
     * @param [type] $password
     * @param [type] $code
     * @param [type] $type
     * @method login
     * @author chengzhigang<1256699215@qq.com>
     */
    public function login($phone, $password, $code, $type)
    {
        try {
            if ($type == 1) {
                //验证验证码
                $key = createCodeKey($phone);
                $correctCode = Cache::pull($key);
                if (empty($correctCode)) {
                    return ['status' => 2, 'msg' => '验证码已失效', 'data' => []];
                }
                if ($correctCode != $code) {
                    return ['status' => 2, 'msg' => '验证码不正确', 'data' => []];
                }
            }
            $userInfo = User::where('phone', $phone)->where('status', 1)->first();
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '账户不存在', 'data' => []];
            }
            //验证密码
            if ($type == 2 && ($userInfo->password != createUserPwd($password))) {
                return ['status' => 2, 'msg' => '密码不正确', 'data' => []];
            }
            //更新用户信息
            if (empty($userInfo->first_login_time)) {
                $userInfo->first_login_time = date('Y-m-d H:i:s');
            }
            $userInfo->login_time = date('Y-m-d H:i:s');
            $userInfo = $userInfo->toArray();
            unset($userInfo['password']);
            User::where('id', $userInfo['id'])->update($userInfo);
            //生成token
            $token = $this->setUserCache($userInfo);
            return ['status' => 1, 'msg' => '登录成功', 'data' => ['token' => $token, 'userInfo' => $userInfo]];
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('用户登录：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 用户注册
     * @param [type] $phone
     * @param [type] $password
     * @param [type] $code
     * @method register
     * @author chengzhigang<1256699215@qq.com>
     */
    public function register($phone, $password, $code)
    {
        try {
            //验证验证码
            $key = createCodeKey($phone);
            $correctCode = Cache::pull($key);
            if (empty($correctCode)) {
                return ['status' => 2, 'msg' => '验证码已失效', 'data' => []];
            }
            if ($correctCode != $code) {
                return ['status' => 2, 'msg' => '验证码不正确', 'data' => []];
            }
            $userInfo = User::where('phone', $phone)->first();
            if (!empty($userInfo)) {
                return ['status' => 2, 'msg' => '账户已存在', 'data' => []];
            }
            //注册用户
            $data['nickname'] = createNickname();
            $data['phone'] = $phone;
            $data['password'] = createUserPwd($password);
            $userModel = User::create($data);
            if ($userModel) {
                return ['status' => 1, 'msg' => '注册成功', 'data' => []];
            } else {
                return ['status' => 2, 'msg' => '注册失败', 'data' => []];
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('用户注册：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 更新用户基本信息
     * @param [type] $token
     * @param [type] $data
     * @method updateInfo
     * @author chengzhigang<1256699215@qq.com>
     */
    public function updateInfo($token, $data)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $userModel = User::where('id', $userInfo['id'])->update($data);
            if ($userModel) {
                foreach ($data as $k => $v) {
                    if (array_key_exists($k, $userInfo)) {
                        $userInfo[$k] = $v;
                    }
                }
                Cache::forever($token, $userInfo); //更新缓存用户信息
                return ['status' => 1, 'msg' => '修改成功', 'data' => []];
            } else {
                return ['status' => 2, 'msg' => '修改失败', 'data' => []];
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('用户修改信息：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 忘记密码
     * @param Request $request
     * @method forgetPwd
     * @author chengzhigang<1256699215@qq.com>
     */
    public function forgetPwd($phone, $password, $code)
    {
        try {
            //验证验证码
            $key = createCodeKey($phone);
            $correctCode = Cache::pull($key);
            if (empty($correctCode)) {
                return ['status' => 2, 'msg' => '验证码已失效', 'data' => []];
            }
            if ($correctCode != $code) {
                return ['status' => 2, 'msg' => '验证码不正确', 'data' => []];
            }
            $userInfo = User::where('phone', $phone)->first();
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '该手机号还未注册', 'data' => []];
            }
            $result = User::where('id', $userInfo->id)->update(['password' => createUserPwd($password)]);
            if ($result) {
                return ['status' => 1, 'msg' => '修改成功', 'data' => []];
            } else {
                return ['status' => 2, 'msg' => '修改失败', 'data' => []];
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('忘记密码：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 上传头像
     * @param [type] $token
     * @param [type] $file
     * @method uploadAvatar
     * @author chengzhigang<1256699215@qq.com>
     */
    public function uploadAvatar($token, $file)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $dir = "uploads/avatar"; //头像地址
            $result = upload($file, 'image', ['dir' => $dir]);
            if ($result['status'] == 1) {
                //上传成功
                $userInfo['avatar'] = config('const.img_url') . $result['data'];
                User::where('id', $userInfo['id'])->update(['avatar' => $userInfo['avatar']]);
                Cache::forever($token, $userInfo); //更新缓存用户信息
                return ['status' => 1, 'msg' => '上传成功', 'data' => ['file' => $userInfo['avatar']]];
            } else {
                return $result;
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('修改头像：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 修改密码
     * @param [type] $token
     * @param [type] $oldPassword
     * @param [type] $password
     * @method updatePwd
     * @author chengzhigang<1256699215@qq.com>
     */
    public function updatePwd($token, $oldPassword, $password)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $userInfo = User::where('id', $userInfo['id'])->first();
            if ($userInfo->password != createUserPwd($oldPassword)) {
                return ['status' => 2, 'msg' => '原密码不正确', 'data' => []];
            }
            $result = User::where('id', $userInfo->id)->update(['password' => createUserPwd($password)]);
            if ($result) {
                return ['status' => 1, 'msg' => '修改成功', 'data' => []];
            } else {
                return ['status' => 2, 'msg' => '修改失败', 'data' => []];
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('修改密码：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 注销登录
     * @param [type] $token
     * @method logout
     * @author chengzhigang<1256699215@qq.com>
     */
    public function logout($token)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            Cache::forget($token);
            return ['status' => 1, 'msg' => '注销成功', 'data' => []];
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('注销登录：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * QQ登录
     * @param [type] $openId
     * @param [type] $nickname
     * @param [type] $avatar
     * @param [type] $sex
     * @method qqLogin
     * @author chengzhigang<1256699215@qq.com>
     */
    public function qqLogin($openId, $nickname, $avatar, $sex)
    {
        try {
            $userInfo = User::where('openId', $openId)->first();
            if (empty($userInfo)) {
                //注册用户
                $data = [];
                $data['nickname'] = $nickname;
                $data['avatar'] = $avatar;
                $data['sex'] = $sex;
                $data['openId'] = $openId;
                $userModel = User::create($data);
                if ($userModel) {
                    $data['id'] = $userModel->id;
                    $data['phone'] = ''; //未绑定手机号
                    $token = $this->setUserCache($data);
                    return ['status' => 1, 'msg' => '登录成功', 'data' => ['token' => $token, 'userInfo' => $data]];
                } else {
                    return ['status' => 2, 'msg' => '登录失败', 'data' => []];
                }
            } else {
                //老用户
                $userInfo->login_time = date('Y-m-d H:i:s');
                $userInfo = $userInfo->toArray();
                unset($userInfo['password']);
                User::where('id', $userInfo['id'])->update($userInfo);
                //生成token
                $token = $this->setUserCache($userInfo);
                return ['status' => 1, 'msg' => '登录成功', 'data' => ['token' => $token, 'userInfo' => $userInfo]];
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('QQ登录：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 绑定手机号
     * @param [type] $token
     * @param [type] $phone
     * @param [type] $code
     * @method bindPhone
     * @author chengzhigang<1256699215@qq.com>
     */
    public function bindPhone($token, $phone, $code)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            //验证验证码
            $key = createCodeKey($phone);
            $correctCode = Cache::pull($key);
            if (empty($correctCode)) {
                return ['status' => 2, 'msg' => '验证码已失效', 'data' => []];
            }
            if ($correctCode != $code) {
                return ['status' => 2, 'msg' => '验证码不正确', 'data' => []];
            }
            //查看手机号是否已存在
            $user = User::where('phone', $phone)->first();
            if ($user) {
                return ['status' => 2, 'msg' => '当前手机号已存在', 'data' => []];
            } else {
                //绑定手机号
                $userInfo['phone'] = $phone;
                $userInfo['login_time'] = date('Y-m-d H:i:s');
                $userInfo['first_login_time'] = date('Y-m-d H:i:s');
                User::where('id', $userInfo['id'])->update($userInfo);
                Cache::forever($token, $userInfo); //缓存用户信息
                return ['status' => 1, 'msg' => '绑定成功', 'data' => ['token' => $token, 'userInfo' => $userInfo]];
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('绑定手机号：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 绑定QQ
     * @param [type] $token
     * @param [type] $openId
     * @param [type] $nickname
     * @param [type] $avatar
     * @param [type] $sex
     * @method bindQQ
     * @author chengzhigang<1256699215@qq.com>
     */
    public function bindQQ($token, $openId, $nickname, $avatar, $sex)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            //查看qq是否已存在
            $user = User::where('openId', $openId)->first();
            if ($user) {
                return ['status' => 2, 'msg' => '当前QQ已存在', 'data' => []];
            }
            if (empty($userInfo['avatar'])) {
                $userInfo['avatar'] = $avatar;
            }
            if (empty($userInfo['nickname'])) {
                $userInfo['nickname'] = $nickname;
            }
            if (empty($userInfo['sex'])) {
                $userInfo['sex'] = $sex;
            }
            $userInfo['openId'] = $openId;
            $res = User::where('id', $userInfo['id'])->update($userInfo);
            if ($res) {
                Cache::forever($token, $userInfo); //缓存用户信息
                return ['status' => 1, 'msg' => '绑定成功', 'data' => ['token' => $token, 'userInfo' => $userInfo]];
            } else {
                return ['status' => 2, 'msg' => '绑定失败', 'data' => []];
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('绑定QQ：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    /**
     * 设置token
     * @param [type] $id
     * @method setUserCache
     * @author chengzhigang<1256699215@qq.com>
     */
    public function setUserCache($userInfo)
    {
        $token = createUserToken($userInfo['id']);
        Cache::forever($token, $userInfo); //缓存用户信息
        return $token;
    }

    /**
     * 通过手机号查询用户
     * @param [type] $token
     * @param [type] $phone
     * @method getUserByPhone
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getUserByPhone($token, $phone)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            //查看用户是否已存在
            $user = User::where('phone', $phone)->first();
            if (empty($user)) {
                return ['status' => 2, 'msg' => '该用户不存在', 'data' => []];
            }
            return ['status' => 1, 'msg' => '请求成功', 'data' => ['userInfo' => $user->toArray()]];
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('通过手机号查询用户：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    ################设置###################

    /**
     * 意见反馈
     * @param [type] $token
     * @param [type] $type
     * @param [type] $content
     * @param [type] $contact
     * @param [type] $image
     * @method addFeedback
     * @author chengzhigang<1256699215@qq.com>
     */
    public function addFeedback($token, $type, $content, $contact, $images)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            //上传图片
            $dir = "uploads/feedback"; //反馈图片地址
            $result = uploads($images, 'image', ['dir' => $dir]);
            if ($result['status'] == 2) {
                return $result;
            } else {
                $images = $result['data'];
            }
            //存储数据
            $data = [];
            $data['user_id'] = $userInfo['id'];
            $data['type'] = $type;
            $data['content'] = $content;
            $data['contact'] = $contact;
            $feedbackModel = UserFeedback::create($data);
            if ($feedbackModel) {
                //存储图片
                foreach ($images as $val) {
                    if ($val) {
                        UserFeedbackImage::create(['feedback_id' => $feedbackModel->id, 'image' => $val]);
                    }
                }
                return ['status' => 1, 'msg' => '反馈成功', 'data' => []];
            } else {
                return ['status' => 2, 'msg' => '反馈失败', 'data' => []];
            }
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('意见反馈：' . $e->getMessage());
            writeErrorLog($e);
            return ['status' => 2, 'msg' => config('const.error'), 'data' => []];
        }
    }

    ################收货地址###############
    /**
     * 获取用户默认地址
     * @param [type] $token
     * @method getDefaultAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getDefaultAddress($token)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $user_id = $userInfo['id'];
            //获取用户默认地址
            $address = UserAddress::where('user_id', $user_id)->where('is_default', 2)->select(['id', 'consignee', 'phone', 'city', 'province', 'district', 'address', 'addresses'])->first();
            if (empty($address)) {
                $address = [];
            } else {
                $address = $address->toArray();
            }
            return ['status' => 1, 'msg' => '请求成功', 'data' => $address];
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('获取用户默认地址：' . $e->getMessage());
            return ['status' => 2, 'msg' => '服务异常', 'data' => []];
        }
    }

    /**
     * 获取用户收货地址
     * @param [type] $token
     * @method getUserAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getUserAddress($token, $address_id)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $user_id = $userInfo['id'];
            //获取用户默认地址
            $address = UserAddress::where('user_id', $user_id)->where('id', $address_id)->first();
            if (empty($address)) {
                $address = [];
            } else {
                $address = $address->toArray();
            }
            return ['status' => 1, 'msg' => '请求成功', 'data' => $address];
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('获取用户收货地址：' . $e->getMessage());
            return ['status' => 2, 'msg' => '服务异常', 'data' => []];
        }
    }

    /**
     * 用户地址列表
     * @param [type] $token
     * @method addressList
     * @author chengzhigang<1256699215@qq.com>
     */
    public function addressList($token)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $user_id = $userInfo['id'];
            //获取用户地址
            $list = UserAddress::where('status', 1)->where('user_id', $user_id)
                ->select(['id', 'consignee', 'phone', 'city', 'province', 'district', 'address', 'addresses', 'is_default', 'create_time', 'update_time'])
                ->orderBy('is_default', 'desc')->orderBy('update_time', 'desc')
                ->get()->toArray();
            return ['status' => 1, 'msg' => '请求成功', 'data' => $list];
        } catch (\Exception $e) {
            Log::info($e->getCode());
            Log::info($e->getFile());
            Log::info($e->getLine());
            Log::info('获取用户地址列表：' . $e->getMessage());
            return ['status' => 2, 'msg' => '服务异常', 'data' => []];
        }
    }

    /**
     * 新增用户收货地址
     * @param [type] $token
     * @param [type] $consignee
     * @param [type] $phone
     * @param [type] $province
     * @param [type] $city
     * @param [type] $district
     * @param [type] $address
     * @param [type] $is_default
     * @method addAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function addAddress($token, $consignee, $phone, $province, $city, $district, $address, $is_default = 1)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $user_id = $userInfo['id'];
            $result = Region::getAddress($province, $city, $district, $address);
            if ($result['status'] == 2) {
                return ['status' => 2, 'msg' => $result['msg'], 'data' => []];
            }
            $addresses = $result['data'];
            DB::beginTransaction(); //开启事务
            //查询是否是首次添加
            $addressInfo = UserAddress::where('user_id', $user_id)->first();
            if (empty($addressInfo)) {
                $is_default = 2;
            }
            //新增地址
            $data = [];
            $data['user_id'] = $user_id;
            $data['consignee'] = $consignee;
            $data['phone'] = $phone;
            $data['province'] = $province;
            $data['city'] = $city;
            $data['district'] = $district;
            $data['address'] = $address;
            $data['addresses'] = $addresses;
            $data['is_default'] = $is_default;
            $addressModel = UserAddress::create($data);
            if ($addressModel) {
                //更改默认地址
                if ($is_default == 2) {
                    UserAddress::where('user_id', $user_id)->where('id', '<>', $addressModel->id)->update(['is_default' => 1]);
                }
                DB::commit(); //提交
                $data['id'] = $addressModel->id;
                return ['status' => 1, 'msg' => '新增成功', 'data' => $data];
            } else {
                return ['status' => 2, 'msg' => '新增失败', 'data' => []];
            }
        } catch (\Exception $e) {
            DB::rollback(); //回滚
            Log::info('新增用户地址：' . $e->getMessage());
            return ['status' => 2, 'msg' => '服务异常', 'data' => []];
        }
    }

    /**
     * 编辑收货地址
     * @param [type] $token
     * @param [type] $consignee
     * @param [type] $phone
     * @param [type] $province
     * @param [type] $city
     * @param [type] $district
     * @param [type] $address
     * @param int $is_default
     * @param [type] $address_id
     * @method editAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function editAddress($token, $consignee, $phone, $province, $city, $district, $address, $is_default = 1, $address_id)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $user_id = $userInfo['id'];
            $result = Region::getAddress($province, $city, $district, $address);
            if ($result['status'] == 2) {
                return ['status' => 2, 'msg' => $result['msg'], 'data' => []];
            }
            $addresses = $result['data'];
            DB::beginTransaction(); //开启事务
            //查询是否是首次添加
            $addressCount = UserAddress::where('user_id', $user_id)->count();
            if ($addressCount == 1) {
                $is_default = 2;
            }
            //新增地址
            $data = [];
            $data['consignee'] = $consignee;
            $data['phone'] = $phone;
            $data['province'] = $province;
            $data['city'] = $city;
            $data['district'] = $district;
            $data['address'] = $address;
            $data['addresses'] = $addresses;
            $data['is_default'] = $is_default;
            $res = UserAddress::where('id', $address_id)->where('user_id', $user_id)->update($data);
            if ($res) {
                //更改默认地址
                if ($is_default == 2) {
                    UserAddress::where('user_id', $user_id)->where('id', '<>', $address_id)->update(['is_default' => 1]);
                }
                DB::commit(); //提交
                $data['id'] = $address_id;
                return ['status' => 1, 'msg' => '编辑成功', 'data' => $data];
            } else {
                return ['status' => 2, 'msg' => '编辑失败', 'data' => []];
            }
        } catch (\Exception $e) {
            DB::rollback(); //回滚
            Log::info('编辑用户地址：' . $e->getMessage());
            return ['status' => 2, 'msg' => '服务异常', 'data' => []];
        }
    }

    /**
     * 设置默认地址
     * @param [type] $token
     * @param [type] $address_id
     * @method setDefaultAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function setDefaultAddress($token, $address_id)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $user_id = $userInfo['id'];
            DB::beginTransaction(); //开启事务
            $res = UserAddress::where('user_id', $user_id)->update(['is_default' => 1]);
            $result = UserAddress::where('user_id', $user_id)->where('id', $address_id)->update(['is_default' => 2]);
            if ($res && $result) {
                DB::commit(); //提交
                return ['status' => 1, 'msg' => '设置成功', 'data' => []];
            } else {
                DB::rollback(); //回滚
                return ['status' => 2, 'msg' => '设置失败', 'data' => []];
            }
        } catch (\Exception $e) {
            Log::info('设为默认地址：' . $e->getMessage());
            return ['status' => 2, 'msg' => '服务异常', 'data' => []];
        }
    }

    /**
     * 删除收货地址
     * @param [type] $token
     * @param [type] $address_id
     * @method  delAddress
     * @author chengzhigang<1256699215@qq.com>
     */
    public function delAddress($token, $address_id)
    {
        try {
            //验证token
            $userInfo = Cache::get($token);
            if (empty($userInfo)) {
                return ['status' => 2, 'msg' => '请登录应用', 'data' => []];
            }
            $user_id = $userInfo['id'];
            $res = UserAddress::where('user_id', $user_id)->where('id', $address_id)->update(['status' => 2]);
            if ($res) {
                return ['status' => 1, 'msg' => '删除成功', 'data' => []];
            } else {
                return ['status' => 2, 'msg' => '删除失败', 'data' => []];
            }
        } catch (\Exception $e) {
            Log::info('删除用户地址：' . $e->getMessage());
            return ['status' => 2, 'msg' => '服务异常', 'data' => []];
        }
    }
}
